package com.pan.alg.leecode;
import java.util.ArrayList;
/**
 * @author by panstark
 * @description
 * @notice
 * @date 2022/1/17
 */
public class SpiralOrder {


    public static ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> result = new ArrayList<>();
        if(matrix.length==0){
            return result;
        }
        int rowBegin = 0;
        int rowEnd = matrix.length-1;
        int colBegin = 0;
        int colEnd = matrix[0].length-1;

        while(rowBegin<=rowEnd && colBegin<=colEnd){

            //向右遍历
            for(int i=colBegin ;i<=colEnd;i++){
                result.add(matrix[rowBegin][i]);
            }
            rowBegin++;

            //向下遍历
            for(int i=rowBegin;i<=rowEnd;i++){
                result.add(matrix[i][colEnd]);
            }
            colEnd--;

            //向左遍历
            for(int i=colEnd;i>=colBegin;i--){
                result.add(matrix[rowEnd][i]);
            }

            rowEnd--;
            //向上遍历
            for(int i=rowEnd;i>=rowBegin;i--){
                result.add(matrix[i][colBegin]);
            }

            colBegin++;
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] testInt = {{1,2,3},{4,5,6},{7,8,9}};

        ArrayList<Integer> integers = spiralOrder(testInt);
        System.out.println(integers);

    }

}
